package com.example.ixform.controllers;


import com.example.ixform.dao.IdolRepository;
import com.example.ixform.dao.ProductionRepository;
import com.example.ixform.dto.ChartData;
import com.example.ixform.dto.IdolProduction;
import com.example.ixform.entities.Production;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


import java.util.List;


@Controller
public class MainController {

    @Autowired
    ProductionRepository productionRepository;

    @Autowired
    IdolRepository idolRepository;
    @GetMapping("/main")
    public String displayMain(Model model) throws JsonProcessingException {
//       model.addAttribute("versionNumber",version);

       List<Production> productions = productionRepository.findAll();
       model.addAttribute("productions",productions);

       List<ChartData> productionStatusData = productionRepository.getProductionStatus();
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(productionStatusData);

        model.addAttribute("productionStatusData", jsonString);

        List<IdolProduction> idolProduction= idolRepository.idolProductions();
        model.addAttribute("idolProduction",idolProduction);
        return "mains/main";

    }
}
